<?php

$cs = Yii::app()->clientScript;
$this->Widget('application.extensions.jsTree.CjsTree', array (
	'htmlOptions' => array (
		'id' => 'b_project_topic_tree'
	),
	//'ui' => array (
	//	'theme_name' => 'checkbox'
	//),
	//'options' => array (
	//	'plugins' => array (
	//		'checkbox' => array (
	//			'three_state' => 'true'
	//		)
	//	)
	//),
	'genDiv' => false,
	'callback' => array (
		'onload' => "js:function() { \n" .
		"}"
	)
));

echo CHtml::activeHiddenField($topicValidationForm, 'project_id') . "\n";
echo CHtml::activeHiddenField($topicValidationForm, 'id') . "\n";
$form_name = 'TopicValidationForm[topics]';
?>

<div class="yiiForm" id="project_topic">
	<?php echo CHtml::errorSummary($topicValidationForm); ?>

	<div id="b_project_topic_tree" class="topic_tree">
	<?php displaySteps($this, $form_name, $topicValidationForm->steps); ?>
	</div><!-- project_topic_tree -->
</div><!-- yiiForm -->

<?php

function displaySteps($p, $topicValidationForm, $steps) {
	if (is_array($steps) && count($steps) > 0) {
		echo "<ul>";
		foreach ($steps as $step) {
			echo "<li id='b_node_" . $step->id . "' class='open node'>\n";
			echo "\t<a href='#' class='node'>" . $step->label . "</a>";
			displayTopics($p, $topicValidationForm, $step->topics);
			echo "</li>";
		}
		echo "</ul>";
	}
}

function displayTopics($p, $topicValidationForm, $topics) {
	$image = "";

	if (is_array($topics) && count($topics) > 0) {
		echo "<ul>";
		foreach ($topics as $topic) {
			if ($topic->topic->adviser_validation || $topic->topic->owner_validation_topic) {
				$image = CHtml::image('images/arrow_down_blue.png', 'Show/Hide validation comments', array (
					'id' => 'img_comment_' . $topic->project_topic_id
				));
				echo "<li id='b_node_" . $topic->project_topic_id . "' class='node'>\n" .
				"\t\n\t$image\n\t" .
				"\n\t<a href='#' class='node'>" . $topic->label . "</a>";
				$p->widget('adviser.widgets.CommentLayer', array (
					'topic_id' => $topic->project_topic_id,
					'columns' => 60,
					'rows' => 5,
					'prefix' => 'b_',
					'adviser_validation' => $topic->topic->adviser_validation,
					'adviser_topic_validation' => $topic->topic->adviser_validation_topic,
					'owner_validation' => $topic->topic->owner_validation,
					'owner_topic_validation' => $topic->topic->owner_validation_topic,
					'comment' => $topic->topic->validation_comments,
					'form_name' => $topicValidationForm,
					'read_only' => true
				));
			}else{
				echo "<li id='b_node_" . $topic->project_topic_id . "' class='node'>\n" . $topic->label ;
			}
			displayTopics($p, $topicValidationForm, $topic->children);
			echo "</li>";
		}
		echo "</ul>";
	}
}

$script =<<<JS
init_topics($("#b_project_topic_tree") ,"b_");
$("#b_project_topic_tree").parent().append('<hr class="separation" />');
JS;

$cs->registerScript('b_topics_form_save', $script);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/masks.js', CClientScript::POS_HEAD);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/topic_validation.js', CClientScript::POS_HEAD);
?>
